// 发布博客
$(function () {
    $('#classify').on('changed.bs.select', function (e) {
        $("#chooseclassify").val($(this).val().join(','));
    });
    // 上传图片并预览
    var inputpic = document.getElementById("inputfile")
    inputpic.onchange = function () {
        var file = this.files[0]
        var imageUrl = URL.createObjectURL(file);
        document.getElementById("willsetImg").src = imageUrl;
        document.getElementById("willsetImg").style = "width:200px,height:200px";
    }
    $("form").submit(e => {
        e.preventDefault()
        // var formData = {
        //     title: $('#title').val(),
        //     author: $('#author').val(),
        //     classify: $('#chooseclassify').val(),
        //     editorValue: ue.getContent()
        // }
        var formData = new FormData($("form")[0])
        // console.log(formData);
        $.ajax({
            url: '/writeblogs',
            type: 'post',
            data: formData,
            // dataType: 'json',
            cache: false,
            contentType: false,   //已经在表单的entype处声明了multipart/form-data
            processData: false,  //data值是FormData对象，不需要对数据做处理
            success: (d) => {
                console.log("success");
                switch (d.code) {
                    case 1:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 2:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 3:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 4:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                    case 5:
                        swal.fire({ title: d.msg, type: "success", confirmButtonText: '进入博客管理页' }).then(() => {
                            window.location.href = "/manageblogs"
                        })
                        break;
                    case 6:
                        swal.fire({ title: d.msg, type: "error" })
                        break;
                }
            },
            error: () => {
                swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                    window.location.href = "/login"
                })
            }
        })
    })
})